package handler.logFormHandlers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;

import javax.swing.JComboBox;
import javax.swing.JTable;

import customGUI.selectors.ComboBoxAssetSelector;


import db.LogMethods;


public class LFComboBoxAssetSelectorListener implements ActionListener{

	private JTable logTable;
	private JComboBox<String> selector;
	
	public LFComboBoxAssetSelectorListener(JTable logTable, JComboBox<String> selector){
		this.logTable = logTable;
		this.selector = selector;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		LogMethods logMethods = new LogMethods();
		
		if(selector.getSelectedItem() == null)
			return;
		
		if(selector.getSelectedItem().toString().equals("All"))
			logMethods.ViewLog(logTable);
		else
			logMethods.ViewLogByID(logTable, extractID(selector.getSelectedItem().toString()));
	}
	
	private int extractID(String string){
		StringTokenizer tokenizer = new StringTokenizer(string, "-");
		tokenizer.nextToken(); //go past the name
		return Integer.parseInt(tokenizer.nextToken());
	}

}
